package com.zy.qudadid.presenter;


import com.zy.qudadid.model.Res;
import com.zy.qudadid.network.Const;
import com.zy.qudadid.network.Net;
import com.zy.qudadid.presenter.base.BasePresenterImp;
import com.zy.qudadid.ui.view.RegisterCUploadView;
import com.zy.qudadid.utils.ToolsUtil;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ninos on 2016/7/30.
 */
public class RegisterCUploadPresenter extends BasePresenterImp<RegisterCUploadView> {

    public void DriverImage(String phoneNumber, String path, String type) {
        File file = new File(path);

        // create RequestBody instance from file
        RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("imgsrc", file.getName(), requestFile);

        MultipartBody.Part mobile = MultipartBody.Part.createFormData("mobile", phoneNumber);
        MultipartBody.Part t = MultipartBody.Part.createFormData("type", type);

        Subscription subscription = Net.getService()
                .post_Qualifications(body, mobile, t)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Res>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.errorDriverImage();
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Res res) {
                        if (res.code == Const.OK) {

                        } else {

                        }
                    }
                });

        addSubscription(subscription);

    }

    public void DriverImageHeader(String phoneNumber, String path, String type) {
        File file = new File(path);

        // create RequestBody instance from file
        RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("imgsrc", file.getName(), requestFile);

        MultipartBody.Part mobile = MultipartBody.Part.createFormData("mobile", phoneNumber);
        MultipartBody.Part t = MultipartBody.Part.createFormData("type", type);

        Subscription subscription = Net.getService()
                .post_Qualifications(body, mobile, t)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Res>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToolsUtil.print("----","注册失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Res res) {
                        if (res.code == Const.OK) {
                            view.successRegister();
                        } else {
                            view.errorRegister(res.message);
                        }
                    }
                });

        addSubscription(subscription);

    }
}
